/**
 * Copyright (c) 2018 软唐 All rights reserved.
 *
 * https://www.softtown.cn
 *
 * 版权所有，侵权必究！
 */

package com.softtown.modules.dapai.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.dapai.dao.CompOutboundOrderDetailDao;
import com.softtown.modules.dapai.dto.*;
import com.softtown.modules.dapai.entity.CompMacEntity;
import com.softtown.modules.dapai.entity.CompOutboundOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompOutboundOrderEntity;
import com.softtown.modules.dapai.service.CompMacService;
import com.softtown.modules.dapai.service.CompOutboundOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 出库单
* @date: 2022/1/2 20:47
* @Param:
* @return:
*/
@RestController
@RequestMapping("outboundOrder")
@Api(tags="出库单")
public class CompOutboundOrderController {

    @Resource
    private CompOutboundOrderService compOutboundOrderService;

    @Resource
    private CompMacService compMacService;

    @PostMapping("getOutboundOrderPageList")
    @ApiOperation("获取出库单分页")
    public Result getOutboundOrderPageList(@RequestBody CompOutboundOrderDTO compOutboundOrderDTO) {
        PageInfo<CompOutboundOrderDTO> compOutboundOrderDTOPageInfo = compOutboundOrderService.getOutboundOrderPageList(compOutboundOrderDTO);
        return new Result<PageInfo<CompOutboundOrderDTO>>().ok(compOutboundOrderDTOPageInfo);
    }



    @PostMapping("getOutboundOrderById")
    @ApiOperation("获取出库单详情")
    public Result<CompOutboundOrderAddDTO> getOutboundOrderById(@RequestBody CompOutboundOrderDTO compOutboundOrderDTO) {
        CompOutboundOrderAddDTO compOutboundOrderAddDTO = compOutboundOrderService.getOutboundOrderById(compOutboundOrderDTO.getCompId(),compOutboundOrderDTO.getId());
        return new Result<CompOutboundOrderAddDTO>().ok(compOutboundOrderAddDTO);
    }


    @PostMapping("addOutboundOrder")
    @ApiOperation("添加出库单")
    public Result addOutboundOrder(@RequestBody CompOutboundOrderAddDTO compOutboundOrderAddDTO) {
        Result result = new Result<>();
        CompOutboundOrderEntity compOutboundOrderEntity = compOutboundOrderAddDTO.getCompOutboundOrderEntity();
        List<CompOutboundOrderDetailEntity> compOutboundOrderDetailDTOList =  compOutboundOrderAddDTO.getCompOutboundOrderDetailEntityList();
        for (CompOutboundOrderDetailEntity entity: compOutboundOrderDetailDTOList){
            CompMacAddDTO compMacAddDTO = compMacService.getMacById(compOutboundOrderEntity.getCompId(), entity.getMacScrewId());
            CompMacEntity compMacEntity =ConvertUtils
                    .sourceToTarget(compMacAddDTO.getCompMacEntity(), CompMacEntity.class);
            // 出库后,库存数量小于最小库存数量
            if (!StringUtils.isEmpty(compMacEntity)){
                if ((compMacEntity.getStock() - compMacEntity.getMinInventory()) < entity.getOutboundQuantity()){
                    return result.error("库存数量不能小于最小库存数量");
                }else {
                    CompMacEntity compMacEntity1 = new CompMacEntity();
                    compMacEntity1.setId(compMacEntity.getId());
                    compMacEntity1.setCompId(compMacEntity.getCompId());
                    compMacEntity1.setStock(compMacEntity.getStock() - entity.getOutboundQuantity());
                    compOutboundOrderService.updateMacStock(compMacEntity1);
                }
            }else {
                return result.error("设备不存在");
            }


        }
        compOutboundOrderService.addOutboundOrder(compOutboundOrderAddDTO);
        return result;
    }

    @PostMapping("updateOutboundOrder")
    @ApiOperation("修改出库单")
    public Result updateOutboundOrder(@RequestBody CompOutboundOrderAddDTO compOutboundOrderAddDTO) {
        Result result = new Result<>();
        CompOutboundOrderEntity compOutboundOrderEntity = compOutboundOrderAddDTO.getCompOutboundOrderEntity();
        List<CompOutboundOrderDetailEntity> compOutboundOrderDetailDTOList =  compOutboundOrderAddDTO.getCompOutboundOrderDetailEntityList();
        for (CompOutboundOrderDetailEntity entity: compOutboundOrderDetailDTOList){
            CompMacAddDTO compMacAddDTO = compMacService.getMacById(compOutboundOrderEntity.getCompId(), entity.getMacScrewId());
            CompMacEntity compMacEntity =ConvertUtils
                    .sourceToTarget(compMacAddDTO.getCompMacEntity(), CompMacEntity.class);
            // 出库后,库存数量小于最小库存数量
            if (!StringUtils.isEmpty(compMacEntity)){
                if ((compMacEntity.getStock() - compMacEntity.getMinInventory()) < entity.getOutboundQuantity()){
                    return result.error("库存数量不能小于最小库存数量");
                }else {
                    CompMacEntity compMacEntity1 = new CompMacEntity();
                    compMacEntity1.setId(compMacEntity.getId());
                    compMacEntity1.setCompId(compMacEntity.getCompId());
                    compMacEntity1.setStock(compMacEntity.getStock() - entity.getOutboundQuantity());
                    compOutboundOrderService.updateMacStock(compMacEntity1);
                }
            }else {
                return result.error("设备不存在");
            }


        }
        compOutboundOrderService.updateOutboundOrder(compOutboundOrderAddDTO);
        return result;
    }

    @PostMapping("deleteOutboundOrder")
    @ApiOperation("删除出库单")
    public Result deleteOutboundOrder(@RequestBody CompOutboundOrderDTO compOutboundOrderDTO) {
        Result result = new Result<>();
        compOutboundOrderService.deleteOutboundOrder(compOutboundOrderDTO);
        return result;
    }

    @PostMapping("getOutboundOrderDetailPageList")
    @ApiOperation("获取出库单详情分页")
    public Result getOutboundOrderDetailPageList(@RequestBody CompOutboundOrderDetailDTO compOutboundOrderDetailDTO) {
        PageInfo<CompOutboundOrderDetailDTO> compOutboundOrderDetailDTOPageInfo = compOutboundOrderService.getOutboundOrderDetailPageList(compOutboundOrderDetailDTO);
        return new Result<PageInfo<CompOutboundOrderDetailDTO>>().ok(compOutboundOrderDetailDTOPageInfo);
    }

}
